//
//  WHStatusOriginalView.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/11.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

let StatusCellMargin: CGFloat = 8
class WHStatusOriginalView: UIView {

    //视图底部的约束
    var bottomCons: Constraint?
    
    var statusViewModel: WHStatusViewModel?{
        didSet{
            
            //绑定模型数据
            let headImageURL = statusViewModel?.headImageURL
            iconView.sd_setImageWithURL(headImageURL)
            nameLabel.text = statusViewModel?.status?.user?.name
            
            //用户等级
            mbRankImageView.image = statusViewModel?.mbImage
            
            //用户认证类型
            verifiedImageView.image = statusViewModel?.verifiedImage
            
            //设置时间
            //TODO 后期完善
            //            timeLabel.text = status?.created_at
            //            sourceLabel.text = status?.source
            contentLabel.text = statusViewModel?.status?.text
            
            //清空约束
            bottomCons?.uninstall()
            //cell复用设置
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0{
                //有配图视图
                //设置约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //显示转发微博
                pictureView.hidden = false
                //设置数据
                pictureView.imageURls = urls
            }else{
                //没有
                //设置约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //隐藏转发微博
                pictureView.hidden = true
                //清空数据
                pictureView.imageURls = nil
            }

            
        }
    }
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        steupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func steupUI(){
        backgroundColor = UIColor.whiteColor()
        sep.backgroundColor = UIColor.redColor()
        //圆角的处理
        iconView.cornerRadius = 20
        addSubview(sep)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        //添加配图视图
        addSubview(pictureView)
        //约束
        sep.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sep.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
        }
        //配图视图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            //测试高度
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
//设置原创微博的底部相对微博图片的底部约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        
    }
    ///分隔条
    private lazy var sep: UIView = UIView()
    ///用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    ///用户等级
    private lazy var mbRankImageView: UIImageView = UIImageView(image:UIImage(named: "common_icon_membership"))
    ///用户认证类型
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    ///用户名
    private lazy var nameLabel: UILabel = UILabel(text: "童鞋民", textColor: UIColor.darkGrayColor(),fontSize: 15)
    ///微博时间
    private lazy var timeLabel: UILabel = UILabel(text: "11:11", textColor: UIColor.orangeColor(), fontSize: 12)
    ///微博来源
    private lazy var sourceLabel: UILabel = UILabel(text: "隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 12)
    ///微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "我啊SD卡大声道", textColor: UIColor.darkGrayColor(), fontSize: 15,margin: StatusCellMargin)
    //配图视图
    private lazy var pictureView: WHStatusPictureView = WHStatusPictureView()
}
